package com.org.zp.ctrl.myzhaopin;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.entity.login.Resume;
import com.org.zp.entity.myzp.ResumeInfo;

public class EditResumeActivity extends BaseActivity {
	private RecApp mApp;

	private TextView headerTitle;
	private Button backbutton, top_right_btn;

	private EditText myzp_create_resume_name, myzp_create_resume_user_name,
			myzp_create_resume_year, myzp_create_resume_height,
			myzp_create_resume_account, myzp_create_resume_phone,
			myzp_create_resume_email, myzp_create_resume_address;

	private TextView myzp_create_resume_edu;
	private TextView myzp_create_resume_exp;

	private Button myzp_create_resume_bt;

	private RadioGroup myzp_create_resume_sex_radio;
	private RadioButton myzp_create_resume_sex_man,
			myzp_create_resume_sex_women;

	private ImageView usercenter_icon_header;

	private static final int CROP_FROM_CAMERA = 2;

	private Bitmap bitmap;

	private String mSex = "1";

	private ProgressDialog progressDialog;
	
	private ResumeInfo mResume = new ResumeInfo();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myzhaopin_create_resume);
		mApp = getMyApplication();

		if (!(ActivityTaskManager.getInstance()
				.containsName("EditResumeActivity"))) {
			ActivityTaskManager.getInstance().putActivity(
					"EditResumeActivity", this);
		}

		mResume = (ResumeInfo) getIntent().getSerializableExtra("resume");
		
		// 返回按钮
		backbutton = (Button) findViewById(R.id.back);
		backbutton.setText("  返回");
		backbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"EditResumeActivity");
			}
		});
		// 标题
		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("创建简历 ");

		top_right_btn = (Button) findViewById(R.id.top_right_button);
		top_right_btn.setVisibility(View.GONE);

		init();

	}

	private void init() {

		myzp_create_resume_name = (EditText) findViewById(R.id.myzp_create_resume_name);
		myzp_create_resume_name.setText(mResume.getResume_name());
		
		myzp_create_resume_user_name = (EditText) findViewById(R.id.myzp_create_resume_user_name);
		myzp_create_resume_user_name.setText(mResume.getResume_year());
		
		myzp_create_resume_year = (EditText) findViewById(R.id.myzp_create_resume_year);
		myzp_create_resume_year.setText(mResume.getResume_height());
		
		myzp_create_resume_height = (EditText) findViewById(R.id.myzp_create_resume_height);
		myzp_create_resume_height.setText(mResume.getResume_height());
		
		myzp_create_resume_account = (EditText) findViewById(R.id.myzp_create_resume_account);
		myzp_create_resume_account.setText(mResume.getResume_account());
		
		myzp_create_resume_phone = (EditText) findViewById(R.id.myzp_create_resume_phone);
		myzp_create_resume_phone.setText(mResume.getResume_phone());
		
		myzp_create_resume_email = (EditText) findViewById(R.id.myzp_create_resume_email);
		myzp_create_resume_email.setText(mResume.getResume_email());
		
		myzp_create_resume_address = (EditText) findViewById(R.id.myzp_create_resume_address);
		myzp_create_resume_address.setText(mResume.getResume_address());

		myzp_create_resume_edu = (TextView) findViewById(R.id.myzp_create_resume_edu);
		myzp_create_resume_edu.setText(mResume.getResume_edu());
		
		myzp_create_resume_exp = (TextView) findViewById(R.id.myzp_create_resume_exp);
		myzp_create_resume_exp.setText(mResume.getResume_exp());
		

		myzp_create_resume_bt = (Button) findViewById(R.id.myzp_create_resume_bt);
		myzp_create_resume_bt.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				progressDialog = ProgressDialog.show(EditResumeActivity.this,
						"请等待...", "正在提交简历信息，请稍后...", true, true);

				Map<String, Object> params = new HashMap<String, Object>();
				params.put("act", "resumeUpdate");
				params.put("id", mResume.getResume_id());
				params.put("title", myzp_create_resume_name.getText()
						.toString());
				params.put("fullname", myzp_create_resume_user_name.getText()
						.toString());
				params.put("sex", mSex);
				params.put("birthdate", myzp_create_resume_year.getText()
						.toString());
				params.put("height", myzp_create_resume_height.getText()
						.toString());
				// params.put("experience", );
				params.put("householdaddress", myzp_create_resume_account
						.getText().toString());
				// params.put("education", );
				params.put("telephone", myzp_create_resume_phone.getText()
						.toString());
				params.put("email", myzp_create_resume_email.getText()
						.toString());
				params.put("address", myzp_create_resume_address.getText()
						.toString());
				params.put("photo_img", bitmap);
				new Upload().execute(params);
			}
		});

		myzp_create_resume_sex_radio = (RadioGroup) findViewById(R.id.myzp_create_resume_sex_radio);
		myzp_create_resume_sex_radio
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup arg0, int arg1) {
						// TODO Auto-generated method stub
						// 获取变更后的选中项的ID
						int radioButtonId = arg0.getCheckedRadioButtonId();
						switch (radioButtonId) {
						case R.id.myzp_create_resume_sex_man:
							mSex = "1";
							break;
						case R.id.myzp_create_resume_sex_women:
							mSex = "2";
							break;
						}
					}
				});
		myzp_create_resume_sex_man = (RadioButton) findViewById(R.id.myzp_create_resume_sex_man);
		myzp_create_resume_sex_women = (RadioButton) findViewById(R.id.myzp_create_resume_sex_women);
		
		if("1".equals(mResume.getResume_sex())){
			mSex = "1";
			myzp_create_resume_sex_man.setChecked(true);
		}else{
			mSex = "2";
			myzp_create_resume_sex_women.setChecked(true);
		}

		usercenter_icon_header = (ImageView) findViewById(R.id.myzp_create_resume_usercenter_icon_header);


	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"EditResumeActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	class Upload extends AsyncTask<Map<String, Object>, Void, String> {

		@Override
		protected String doInBackground(Map<String, Object>... params) {
			Log.e("TMG", "create resume post ");
			return HttpUtils.postImage(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			progressDialog.dismiss();
			try {
				String succeed = new JSONObject(result).getString("succeed");
				if (succeed.equals("000")) {
					Resume resume = new Resume();
					
					Toast toast = Toast.makeText(EditResumeActivity.this,
							"修改成功", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					ActivityTaskManager.getInstance().removeActivity(
							"EditResumeActivity");
				} else {
					Toast toast = Toast.makeText(EditResumeActivity.this,
							"修改失败", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		protected void onCancelled() {
		}

	}
}
